package com.drako.tdgdx;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.drako.tdgdx.helper.CamController;
import com.drako.tdgdx.helper.GlobalHelper;
import com.drako.tdgdx.helper.VisualDebugHelper;
import com.drako.tdgdx.input.InputHandler;
import com.drako.tdgdx.logic.GameLogic;
import com.drako.tdgdx.logic.PlayingFieldDiffusion;
import com.drako.tdgdx.logic.creep.Creep;
import com.drako.tdgdx.logic.turret.Tower;


public class TdGdx implements ApplicationListener{
	
	private OrthographicCamera camera;
	private PerspectiveCamera cam;

	private GL10 gl;

	private SpriteBatch batch;
	private BitmapFont font;
	
	
	
	private Vector2 cameraPos;
	
	private GameLogic logic;
	private InputHandler input;
	
	@Override
	public void create() {
		Gdx.app.log("Create", "Start Init");
		long time = System.currentTimeMillis();
		logic = new GameLogic();
		camera = new OrthographicCamera( 500, 300);
		cam = new PerspectiveCamera(67, 500, 300);
		cam.far= 300;

		font = new BitmapFont(Gdx.files.internal("data/fontarial.fnt"), Gdx.files.internal("data/fontarial.png"), false);
		gl = Gdx.graphics.getGL10();
		batch = new SpriteBatch();
		//PlayingFieldDiffusion Test
		
		//sets starting Camera position  to the middle of the playingfield
		cameraPos= GlobalHelper.midOfPlayingField();
		cameraPos.sub(cam.position.x,cam.position.y);
		cam.translate(cameraPos.x, cameraPos.y, 100);
		
		
		//control touchdrag cam position
		input = new InputHandler(cam,logic);
		Gdx.input.setInputProcessor(input);
		time = System.currentTimeMillis()-time;
		Gdx.app.log("Create", "Finished Init after"+time);
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	public void update(){
		logic.update();
	}
	@Override
	public void render() {
		GlobalHelper.tickUp(Gdx.graphics.getDeltaTime());
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		update();
		//uncomment for 3D
				
		cam.update();
		cam.apply(gl);
//		camera.update();
//		camera.apply(gl);
		logic.render(gl);
		VisualDebugHelper.drawPoints(gl);
		
		
		
		batch.begin();
		font.draw(batch, "fps:"+Gdx.graphics.getFramesPerSecond(), cameraPos.x-120,cameraPos.y+ 200);
		batch.end();
		
		
		
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}



	
	

}
